import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

import '../../utils/string_tool.dart';

const String fPrefixFolder = 'assets/images/';
const String fCommonFolder = 'assets/images/common/';
const String fLoginFolder = 'login';

/// 加载本地资源图片
class JDAssetImage extends StatelessWidget {
  const JDAssetImage(this.image,
      {super.key,
      this.size,
      this.width,
      this.height,
      this.fit,
      this.color,
      this.circular = 0});

  // 设置size后不用宽高
  final Size? size;
  final String image;
  final double? width;
  final double? height;
  final BoxFit? fit;
  final Color? color;
  final double circular;

  @override
  Widget build(BuildContext context) {
    Widget child = Image.asset(
      TDImageTool.getImgPath(image),
      height: size?.height ?? height,
      width: size?.width ?? width,
      fit: fit,
      color: color,
    );
    if (circular > 0) {
      return ClipRRect(
        borderRadius: BorderRadius.circular(circular),
        child: child,
      );
    } else {
      return child;
    }
  }
}

class TDImageTool {
  static ImageProvider getAssetImage(
    String name,
  ) {
    return AssetImage(getImgPath(name));
  }

  static String getImgPath(String name) {
    if (name.endsWith('.png')) {
      return 'assets/images/$name';
    } else {
      return 'assets/images/$name.png';
    }
  }

  static ImageProvider getImageProvider(String? imageUrl,
      {String holderImg = 'none'}) {
    if (StringTool.isEmpty(imageUrl)) {
      return AssetImage(getImgPath(holderImg));
    }
    return CachedNetworkImageProvider(imageUrl!);
  }
}
